package com.abe.mvp.view.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.abe.config.SettingParam;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.model.bean.SettingBean;
import com.abe.mvp.view.activity.FragmentCallbackListener.SettingCallbackListener;
import com.abe.mvp.view.adapter.SettingMenuAdapter;
import com.abe.mvp.view.fragment.base.BaseSettingFragment;
import com.abe.threadmill.R;

@SuppressLint("InflateParams")
public class SettingLanguageFragment extends BaseSettingFragment implements
		OnItemClickListener {

	private SettingCallbackListener callback;

	private ListView language_list_type;
	private List<MenuBean> menuBeans;
	private SettingMenuAdapter adapter;
	private String current_language;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		callback = (SettingCallbackListener) getActivity();
		View view = this.onCreateView(inflater,
				R.layout.activity_set_fragment_language, container,
				savedInstanceState);
		initView(view);
		return view;
	}

	public void initView(View view) {
		language_list_type = (ListView) view
				.findViewById(R.id.language_list_type);
		menuBeans = getBeans();
		adapter = new SettingMenuAdapter(getActivity(), menuBeans, scale,
				SettingMenuAdapter.TYPE_LIST);
		language_list_type.setAdapter(adapter);
		language_list_type.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
		/** 保存设置 */
		if (!menuBeans.get(pos).isSelected()) {
			current_language = SettingParam.languages[pos];
			saveChange();
			/** 刷新显示 */
			menuBeans = getBeans();
			adapter.setListData(menuBeans);
			language_list_type.setAdapter(adapter);
			callback.updateLanguage();
		}
	}

	@Override
	protected void saveChange() {
		SettingBean settingBean = SettingParam.getSettingBean();
		settingBean.setLanguage(current_language);
		SettingParam.updateSettingBean(settingBean);
	}

	private List<MenuBean> getBeans() {
		List<MenuBean> menuBeans = new ArrayList<MenuBean>();
		boolean select[] = { false, false };
		 String[] languages_name = {
				 SettingParam.getStrigById(R.string.common_setting_language_cn),
				 SettingParam.getStrigById(R.string.common_setting_language_en) };
		SettingBean settingBean = SettingParam.getSettingBean();
		current_language = settingBean.getLanguage();
		if (current_language.equals(SettingParam.LANGUAGE_KEY_CN)) {
			select[0] = true;
		} else {
			select[1] = true;
		}
		for (int i = 0; i < SettingParam.languages.length; i++) {
			MenuBean bean = new MenuBean();
			bean.setNameStr(languages_name[i]);
			bean.setSelected(select[i]);
			menuBeans.add(bean);
		}
		return menuBeans;
	}

}
